package com.controller;

import com.entity.User;
import com.packdate.Packdate;
import com.resp.BaseResponseEntity;
import com.service.IUserService;
import com.service.impl.UserServiceimpl;
import com.utlils.ResponseUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 通过名字获取用户信息
 */
@WebServlet("/findByName")
public class FindByNameController extends HttpServlet {
    IUserService service=new UserServiceimpl();


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String name = req.getParameter("name");

        BaseResponseEntity<Boolean> entity=null;
        User user = service.findByName(name);

        //查询出用户之后将用户信息返回
        System.out.println("查询到的用户信息为"+user);
        if(user!=null){
            //打包数据
            entity=new BaseResponseEntity(200,"success",user);
        }else {
            entity= BaseResponseEntity.error();
        }
        ResponseUtils.responseToJson(resp,entity);

    }


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
